//
//  CourseDetailDescView.m
//  TeacherNet
//
//  Created by slz on 2017/11/15.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "CourseDetailDescView.h"
#define kLineColor [UIColor colorWithRGB:0xcdcdcd]

@implementation CourseClassSelectBackView
{
    BOOL _trackingTouch;
}
- (instancetype)init
{
    self = [super init];
    if (self){
        _selectLabel = [UILabel new];
        _selectLabel.fontSize = 15;
        [self addSubview:_selectLabel];
        [_selectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(15);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
        UIImageView * rightArrow = [UIImageView new];
        rightArrow.image = [UIImage imageNamed:@"mine_arrow"];
        [self addSubview:rightArrow];
        [rightArrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.right.mas_equalTo(-10);
            make.size.mas_equalTo(CGSizeMake(16, 16));
        }];
        
        UIView * lineLayer = [UIView new];
        lineLayer.backgroundColor = kLineColor;
        [self addSubview:lineLayer];
        [lineLayer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self);
            make.height.mas_equalTo(1);
        }];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _trackingTouch = NO;
    UITouch *t = touches.anyObject;
    CGPoint p = [t locationInView:self];
    if (CGRectContainsPoint(self.bounds, p)) {
        _trackingTouch = YES;
    }
    if (!_trackingTouch) {
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!_trackingTouch) {
        [super touchesEnded:touches withEvent:event];
    } else {
        if ([_delegate respondsToSelector:@selector(viewClickWithSelectClassId:)]) {
            [_delegate viewClickWithSelectClassId:_classModel.classId];
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!_trackingTouch) {
        [super touchesCancelled:touches withEvent:event];
    }
}

- (void)viewSelectWithModel:(TTClassModel *)model
{
    _classModel = model;
    NSString * aaa = [NSString stringWithFormat:@"已选： %@ 有效期",model.className];
    if (model.classValidityPeriod){
        NSMutableAttributedString * day = [[NSMutableAttributedString alloc]initWithString:model.classValidityPeriod];
        [day addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, day.length)];
        
        NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]initWithString:aaa];
        [attributedString appendAttributedString:day];
        [attributedString appendAttributedString:[[NSAttributedString alloc]initWithString:@"天"]];
        _selectLabel.attributedText = attributedString;
    }
}

@end

@implementation CourseSerciveBackView
{
    BOOL _trackingTouch;
}
- (instancetype)init
{
    self = [super init];
    if (self){
        CGFloat labelWidth = (kScreenWidth - 30)/3;
        CGFloat labelHieght = 20;
        NSArray * data = @[@"系统化学习",@"24H答疑",@"作业批改",@"资料下载",@"配套词场"];
        for (int i = 0; i < data.count; i ++){
            NSString * title = data[i];
            UILabel * label = [UILabel new];
            label.text = title;
            label.textColor = kDark_TextColor_Default;
            label.fontSize = 14;
            label.textAlignment = NSTextAlignmentCenter;
            label.size = CGSizeMake(labelWidth, labelHieght);
            label.left = i > 2?  15 + (labelWidth * (i -3)) : 15 + labelWidth * i;
            label.top = i > 2? 40 : 15;
            [self addSubview:label];
            
            UIView * lineLayer = [UIView new];
            lineLayer.backgroundColor = kLineColor;
            [self addSubview:lineLayer];
            [lineLayer mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.and.bottom.mas_equalTo(self);
                make.height.mas_equalTo(1);
            }];
        }
        
        UIImageView * rightArrow = [UIImageView new];
        rightArrow.image = [UIImage imageNamed:@"mine_arrow"];
        [self addSubview:rightArrow];
        [rightArrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.right.mas_equalTo(-10);
            make.size.mas_equalTo(CGSizeMake(16, 16));
        }];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _trackingTouch = NO;
    UITouch *t = touches.anyObject;
    CGPoint p = [t locationInView:self];
    if (CGRectContainsPoint(self.bounds, p)) {
        _trackingTouch = YES;
    }
    if (!_trackingTouch) {
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!_trackingTouch) {
        [super touchesEnded:touches withEvent:event];
    } else {
        if ([_delegate respondsToSelector:@selector(viewClickWithService)]) {
            [_delegate viewClickWithService];
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!_trackingTouch) {
        [super touchesCancelled:touches withEvent:event];
    }
}

@end

@implementation DetailTimeBackView

-(instancetype)init{
    self = [super init];
    if (self){
        self.backgroundColor = [UIColor whiteColor];
        UILabel * courseTime = [UILabel new];
        courseTime.fontSize = 15;
        courseTime.textColor = kDark_TextColor_Default;
        courseTime.text = @"课程时间:";
        [self addSubview:courseTime];
        [courseTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.top.mas_equalTo(self).offset(14);
        }];
        
        UILabel * priceLabel = [UILabel new];
        priceLabel.fontSize = 15;
        priceLabel.textColor = kDark_TextColor_Default;
        priceLabel.text = @"课程价格:";
        [self addSubview:priceLabel];
        [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(14);
            make.top.mas_equalTo(courseTime.mas_bottom).offset(8);
        }];
        
        _timeLabel = [UILabel new];
        _timeLabel.textColor = kDark_TextColor_Default;
        _timeLabel.fontSize = 15;
        [self addSubview:_timeLabel];
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(courseTime.mas_right).offset(8);
            make.centerY.mas_equalTo(courseTime.mas_centerY);
        }];
        
        _priceLabel = [UILabel new];
        _priceLabel.textColor = [UIColor redColor];
        _priceLabel.fontSize = 15;
        [self addSubview:_priceLabel];
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(priceLabel.mas_right).offset(8);
            make.centerY.mas_equalTo(priceLabel.mas_centerY);
        }];
        
        UIView * lineLayer = [UIView new];
        lineLayer.backgroundColor = kLineColor;
        [self addSubview:lineLayer];
        [lineLayer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self);
            make.height.mas_equalTo(1);
        }];
    }
    return self;
}

@end

@implementation CourseDetailDescView

-(instancetype)init{
    self = [super init];
    if (self){
        self.backgroundColor = [UIColor whiteColor];
       
        
        _timeBackView = [DetailTimeBackView new];
        [self addSubview:_timeBackView];
        [_timeBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.top.mas_equalTo(self);
            make.height.mas_equalTo(75);
        }];
        
        _serciveBackView = [CourseSerciveBackView new];
        [self addSubview:_serciveBackView];
        [_serciveBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(_timeBackView);
            make.top.mas_equalTo(_timeBackView.mas_bottom);
            make.height.mas_equalTo(_timeBackView);
        }];
        
        _selectBackView = [CourseClassSelectBackView new];
        [self addSubview:_selectBackView];
        [_selectBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(_timeBackView);
            make.top.mas_equalTo(_serciveBackView.mas_bottom);
            make.height.mas_equalTo(50);
        }];
        
        _webView = [[WKWebView alloc]init];
        _webView.scrollView.bounces = NO;
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
        [self addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(_timeBackView);
            make.top.mas_equalTo(_selectBackView.mas_bottom);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        
    }
    return self;
}

-(void)setDelegate:(id<CourseDetailDescViewDelegate>)delegate
{
    _selectBackView.delegate = delegate;
    _serciveBackView.delegate = delegate;
}

-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"%@",error);
}

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        
        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
        
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
        
    }
}

-(void)updateWithModel:(CourseDetailDescModel *)model
{
    _timeBackView.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.coursePrice];
    _timeBackView.timeLabel.text = [NSString stringWithFormat:@"%@ --- %@",model.courseStartTime,model.courseEndTime];
    TTClassModel * classModel = [[TTClassModel alloc]init];
    classModel.classValidityPeriod = model.classValidityPeriod;
    classModel.className = model.className;
    classModel.classId = model.classId;
    classModel.classSignUpLastTime = model.courseSignUpLaseTime;
    [_selectBackView viewSelectWithModel:classModel];
}

- (void)loadUrl:(NSString *)url
{
    NSString * urlString = kAPI_IMAGE_HOST(url);
    NSLog(@"%@",urlString);
    NSURL * urllll = [NSURL URLWithString:urlString];
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:urllll];
    [_webView loadRequest:request];
}

//- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
//    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id data, NSError * _Nullable error) {
//        CGFloat height = [data floatValue];
//        //ps:js可以是上面所写，也可以是document.body.scrollHeight;在WKWebView中前者offsetHeight获取自己加载的html片段，高度获取是相对准确的，但是若是加载的是原网站内容，用这个获取，会不准确，改用后者之后就可以正常显示，这个情况是我尝试了很多次方法才正常显示的
//    }];
//}

@end
